#测试博客登陆界面
import time
from selenium.webdriver.common.by import By
from Common.Untils import BlogDriver


class BlogLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

        # 异常登陆测试用例
    def LoginFailTest(self):
        # 空账号空密码登录
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        # 判断弹窗是否符合
        time.sleep(1)
        alert = self.driver.switch_to.alert
        #截图
        BlogDriver.getScreeShot()
        actual = alert.text
        assert actual == "账号或密码不能为空"
        alert.accept()
        # 空账号，正确的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        actual = alert.text
        assert actual == "账号或密码不能为空"
        alert.accept()
        # 正确的账号，空密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        actual = alert.text
        assert actual == "账号或密码不能为空"
        alert.accept()
        # 正确的账号错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1111")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        actual = alert.text
        assert actual == "密码错误"
        alert.accept()
        # 错误的账号正确的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("daimiao")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        actual = alert.text
        assert actual == "用户不存在"
        alert.accept()
        # 错误的账号错误的密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("daimiao")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1111")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        time.sleep(1)
        alert = self.driver.switch_to.alert
        actual = alert.text
        assert actual == "用户不存在"
        alert.accept()

    #成功登陆测试用例
    def LoginSucTest(self):
        #未登录下检测菜单栏按钮是否符合要求
        #点击“主页”,无反应，仍在登陆界面
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(4)").click()
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container-login > div > h3")
        #点击“写博客”跳转到编辑页，点“提交”跳转到登陆页
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        #输入账号密码点击提交
        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        #以“昵称”为基准检验是否登录成功
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3")
        self.driver.back()



# Login=BlogLogin()
# Login.LoginSucTest()
# Login.LoginFailTest()